var NNModel           = require('./NNModel');
var NNProto           = require('./NNProto');
var RoomProto         = require('../../API/RoomProto');
var HallApi           = require('../../API/HallAPI');
var RoomAPI           = require('../../API/RoomAPI');
var RoomMessageRouter = 'game.gameHandler.roomMessageNotify';
var GameMessageRouter = 'game.gameHandler.gameMessageNotify';

cc.Class({
	extends: cc.Component,
	properties: {
		label: cc.Label,
	},

	onLoad: function () {
		this.node.active = false;
		Global.MessageCallback.addListener('RoomMessagePush', this); 
		Global.MessageCallback.addListener('GameMessagePush', this);
		Global.MessageCallback.addListener('ReConnectSuccess', this);
	},

	gameInit: function () {
		this.node.active = (NNModel.getTick() > 0);
	},

	messageCallbackHandler: function(router, msg) {
		if(!NNModel.getGameInited()) { return; }
		if (router === 'RoomMessagePush') {
			if(msg.type === RoomProto.GET_ROOM_SCENE_INFO_PUSH) {
				this.gameInit();
			}
		}
		if(router === 'GameMessagePush') {
			if(msg.type === NNProto.GAME_STATUS_PUSH) {
				this.node.active = (NNModel.getTick() > 0);
			}
		} 
	},

	onDestroy: function () {
		Global.MessageCallback.removeListener('RoomMessagePush', this); 
		Global.MessageCallback.removeListener('GameMessagePush', this);
		Global.MessageCallback.removeListener('ReConnectSuccess', this);
	},

	update: function (dt) {
		if (NNModel.isDismissing()) { return; }
		let tick = Math.floor(NNModel.getTick());
		if (tick > 0) {
			this.node.active = true;
			this.label.string = ((tick <= 9)? '0'+tick:tick);
			NNModel.subTick(dt);
			let curTick = Math.floor(NNModel.getTick());
			if (curTick <= 3 && tick != curTick) {
				Global.AudioManager.playSound('Game/Common/Audio/daojishi1');
			}
		}
		else {
			this.node.active = false;
			/* this.unschedule(this.clock); */
		}
	},
});
